package com.lz.service;

import com.lz.dao.HomeDao;
import com.lz.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by lz on 2018/5/21.
 */
@Service
@Transactional
public class HomeService {

    @Autowired
    private HomeDao homeDao;

    /**
     * 通过期限查询收益率
     * @param period
     */
    public List<Map<String, Object>> queryPeriod(String period) {
        String start = "";
        String end = "";
        switch (period){
            case "1": //1-3月
                start = "30";
                end = "90";
                break;
            case "2": //3-6
                start = "90";
                end = "180";
                break;
            case "3": //6-12
                start = "180";
                end = "365";
                break;
            case "4": // >12
                start = "365";
                end = "10000";
                break;
        }
        return homeDao.queryPeriod(start,end);
    }

    /**
     * 测试题推荐产品
     * @param level
     * @param pageSize
     * @param pageIndex
     */
    public PageUtil recommend(String level, String pageSize, String pageIndex) {
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageSize(Integer.parseInt(pageSize));
        pageUtil.setPageIndex(Integer.parseInt(pageIndex));
        final Map<String, Object> amountMap = this.queryNum();
        Long amount = (Long)amountMap.get("amount");
        pageUtil.setAllRowsAmount(amount.intValue());
        List<Map<String, Object>> recommend = homeDao.recommend(level,pageIndex,pageSize);
        pageUtil.setItems(recommend);
        pageUtil.calculatePage();
        return pageUtil;
    }

    /**
     * 查询定期数量
     * @return
     */
    public Map<String, Object> queryNum(){
        return homeDao.queryNum();
    }
}
